package com.iot.service;

import com.iot.mapper.DRefMapper;
import com.iot.mapper.DTlineMapper;
import com.iot.mapper.TlineMapper;
import com.iot.pojo.MRef;
import com.iot.pojo.MTapp;
import com.iot.pojo.MTline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TlineServiceImpl implements  TlineService {
    @Autowired
    private TlineMapper mapper;
    @Autowired
    private DTlineMapper dmapper;
    @Autowired
    private DRefMapper dRefMapper;
    @Autowired
    private CommonService commonService;
    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }
    @Override
    public int insert(MTline mTline) throws Exception {
        int count = 0;
        count = getRecordCount("AND APP_ID ="+ mTline.getAPP_ID()+"  AND SNAME='" + mTline.getSNAME() + "' AND SITE_ID=" + mTline.getSITE_ID() + "");
        if (count > 0)
            throw new Exception("该名称已存在");
        return mapper.insert(mTline);
    }

    @Override
    public void update(MTline mTline) throws Exception {
        int count = 0;
        count = getRecordCount(" AND APP_ID ="+ mTline.getAPP_ID()+" AND SNAME='" + mTline.getSNAME() + "' AND SITE_ID=" + mTline.getSITE_ID() + " AND LINE_ID!="+mTline.getLINE_ID()+"");
        if (count > 0)
            throw new Exception("该名称已存在");
        mapper.update(mTline);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTline single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTline> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {

        return dmapper.fetchByPage(map);
    }


    @Override
    public Map<String, Object> initList() {
        Map<String, Object> map = new HashMap<>();
        List<MRef> provinceList = dRefMapper.getProvince();
        map.put("province", provinceList);
        return map;
    }

    @Override
    public Map<String, Object> initEdit(int appId) {
        MTapp mTapp = commonService.getApp(appId);
        Map<String, Object> map = new HashMap<>();
        List<MRef> areaList = dRefMapper.getArea(mTapp.getCITY_ID());
        map.put("area", areaList);
        return map;
    }
}
